package com.spider.silence.worker;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.spider.silence.downloader.Njd1Downloader;
import com.spider.silence.output.FileWriter;
import com.spider.silence.output.FileWriterPool;
import com.spider.silence.output.MongoWriter;
import com.spider.silence.output.MongoWriterPool;
import com.spider.silence.pool.Njd1DownloaderPool;
import com.spider.silence.task.Njd1Task;
import com.spider.silence.task.manager.CommonTaskManager;
import com.spider.silence.utils.DateUtils;
import org.apache.log4j.Logger;

import java.io.File;
import java.util.Map;

/**
 * @author Administrator
 * @description 描述
 * @time 2017/4/17 0017
 */
public class Njd1Worker implements  Runnable{
    private Logger logger = Logger.getLogger(Njd1Worker.class);
    @Override
    public void run() {
        while (true&&!Thread.interrupted()){
            Njd1DownloaderPool pool = Njd1DownloaderPool.getInstance();
            Njd1Downloader downloader = null;
            FileWriter writer = null;
            FileWriterPool writerPool =FileWriterPool.getInstance();
            try{
                downloader = pool.fetchDownLoader(1000);
                Njd1Task task = (Njd1Task) CommonTaskManager.takeTask("Njd1",1000);
                if(downloader!=null&&task!=null) {
                    JSONObject content = downloader.crawlItem(task.getUrl());
                    JSONObject data = content.getJSONObject("data");
                    if (data != null && !"".equals(data) && !data.isEmpty()) {
                        String filePrefix = "Njd1";//文件前缀
                        long tsp = DateUtils.dateToTimestamp(DateUtils.parseDateTime(data.getString("publish_time"))).getTime();
                        String user_id = data.getString("publish_user_id");
                        String tid = data.getString("id");
                        //Writer writer =new FTPWriter();
                        String fileName = filePrefix + "_" + tsp + "_" + user_id + "_" + tid;//平台名称（或者英文）_ 言论发布的时间戳 _ 发布用户的ID _ 言论的tid
                        String publishDate = DateUtils.formatDate(DateUtils.parseDate(data.getString("publish_time"))).replace("-", "-");
                        //writer= FTPWriterPool.getInstance().fetchWriter();
                        writer = writerPool.fetchWriter();
                        Map<String,File> map=downloader.getFiles();
                        writer.write("内江第一城_speeches_" + publishDate, fileName, JSON.toJSONString(content, SerializerFeature.BrowserCompatible),map);
                    }
                }
            }catch (InterruptedException e) {
                logger.error(e.getMessage(),e);
                e.printStackTrace();
            } catch (Exception e) {
                logger.error(e.getMessage(),e);
                e.printStackTrace();
            }finally{
                if(downloader!=null) {
                    pool.releaseDownLoader(downloader);
                }
                if(writer!=null) {
                    writerPool.releaseWriter(writer);
                }
            }
        }
    }
}
